﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using IrendilDLL.Database;
using System.Data;
using IrendilDLL;
using System.Data.SqlClient;
using System.IO;

namespace IrendilDLL.utilities
{
    /// <summary>
    /// Classe di gestione Log errori
    /// </summary>
    public class LogErrors
    {
        /// <summary>
        /// Costruttore di default
        /// </summary>
        public LogErrors()
        {
            //sLogFormat used to create log files format :
            // dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
            sLogFormat = DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" ==> ";
            
            //this variable used to create log filename format "
            //for example filename : ErrorLogYYYYMMDD
            string sYear = DateTime.Now.Year.ToString();
            string sMonth = DateTime.Now.Month.ToString();
            string sDay = DateTime.Now.Day.ToString();
            sErrorTime = sYear+sMonth+sDay;

        }

        /// <summary>
        /// Contiene ora e minuti dell'errore
        /// </summary>
        private string sLogFormat;
        /// <summary>
        /// Contiene data dell'errore
        /// </summary>
        private string sErrorTime;


        /// <summary>
        /// Funzione che scrive l'errore su file.
        /// Genera un file al giorno.
        /// </summary>
        /// <param name="sErrMsg">Messaggio di errore</param>
        public void WriteLogToFile(string sErrMsg)
        {
            String str = HttpContext.Current.Server.MapPath("Log");
            StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath("Log/ErrorLog_") + sErrorTime + ".txt", true);
            sw.WriteLine(sLogFormat + sErrMsg);
            sw.Flush();
            sw.Close();
        }


    }
}
